package com.example.nbshopping.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.nbshopping.R;
import com.example.nbshopping.utils.BaseFragment;
import com.example.nbshopping.utils.SaveUserUtils;
import com.example.nbshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;
/**
 * 登录页面
 */
public class LoginFragment extends BaseFragment {
    EditText telEt,pwdEt;
    Button ensureBtn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        initView(view);
        return view;
    }
    private void initView(View view) {
        pwdEt = view.findViewById(R.id.frag_login_et_pwd);
        telEt = view.findViewById(R.id.frag_login_et_tel);
        ensureBtn = view.findViewById(R.id.frag_login_btn);
        ensureBtn.setOnClickListener(listener);
    }
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //获取输入内容
            String tel = telEt.getText().toString();
            String pwd = pwdEt.getText().toString();
            boolean flag = judgeInput(tel,pwd);
            if (flag) { //可以执行网络请求
                Map<String,String> map = new HashMap<>();
                map.put("phone",tel);
                map.put("password",pwd);
                postNetwork(URLUtils.login_url,map);
            }
        }
    };
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:   //登录成功
                Toast.makeText(getContext(),"登录成功！",Toast.LENGTH_SHORT).show();
                UserBean.DataBean data=bean.getData();
                SaveUserUtils.saveUserToFile(getContext(),data);
                Intent intent=new Intent(getContext(),PersonCenterActivity.class);
                startActivity(intent);
                getActivity().finish();
                break;
            default:   //其他，登录失败
                Toast.makeText(getContext(),"登录失败！",Toast.LENGTH_SHORT).show();
                break;
        }

    }
    private boolean judgeInput(String tel, String pwd) {
        //   判断输入内容为空，则不能注册
        if (TextUtils.isEmpty(tel)|| TextUtils.isEmpty(pwd)) {
            Toast.makeText(getContext(),"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }
        //  判断输入的电话号码是否符合要求
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        boolean matches = tel.matches(regex);
        if (!matches){
            Toast.makeText(getContext(),"输入手机号码不合法！",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
